package main

import(
	"net/http"
	"log"
	"encoding/json"
)

func responseWithError(w http.ResponseWriter, code int, msg string){
	if code > 499{
		log.Println("Responding with 5xx error: ,msg")
	}
	type errResponse struct{
		Error string `json:"error"`
		Snack string `json:"Snack"`
	}
	responseWithJson(w,code,errResponse{
		Error: msg,
		Snack: "This is a hard code",
	})
	// {
	// 	"error": "Something went wrong"
	// }
}

func responseWithJson(w http.ResponseWriter, code int, payload interface{}){
	//Go struct -> bytes[]
	data, err := json.Marshal(payload)
	if err != nil{
		log.Println(err)
		w.WriteHeader(500)
		return
	}
	w.Header().Add("Content-Type","application/json")
	w.WriteHeader(code)
	w.Write(data)
}